package com.example.electricbicycle.service;

import com.example.electricbicycle.dao.PurchaseOrderDaoImpl;
import com.example.electricbicycle.dao.VehicleStockDaoImpl;
import com.example.electricbicycle.po.PurchaseOrder;
import com.example.electricbicycle.po.VehicleStock;
import java.util.List;

public class WarehouseServiceImpl implements WarehouseService{
    private PurchaseOrderDaoImpl purchaseOrderDaoImpl = new PurchaseOrderDaoImpl();
    private VehicleStockDaoImpl vehicleStockDaoImpl = new VehicleStockDaoImpl();

    // 审核订单并入库
    public boolean approveAndStockIn(int orderId) {
        PurchaseOrder order = getOrderDetails(orderId);
        if (order == null || order.getStatus() != 0) {
            return false;
        }

        // 更新订单状态
        if (!purchaseOrderDaoImpl.approveOrder(orderId)) {
            return false;
        }

        // 更新库存
        VehicleStock stock = vehicleStockDaoImpl.getStockByVId(order.getV_id());
        int newQuantity = (stock != null) ? stock.getQuantity() + order.getQuantity() : order.getQuantity();

        if (stock == null) {
            // 插入新库存记录
            return vehicleStockDaoImpl.addStock(order.getV_id(), newQuantity);
        } else {
            return vehicleStockDaoImpl.updateStock(stock.getV_id(), newQuantity);
        }
    }

    // 获取订单详情（完整实现）
    public PurchaseOrder getOrderDetails(int orderId) {
        return purchaseOrderDaoImpl.getOrderById(orderId);
    }

    // 分页查询待审核订单
    public List<PurchaseOrder> getPendingOrders(int page, int pageSize) {
        return purchaseOrderDaoImpl.getPendingOrders(page, pageSize);
    }

    // 获取待审核订单总数
    public int getTotalPendingOrders() {
        return purchaseOrderDaoImpl.getTotalPendingOrders();
    }
}